<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <div class="btn-group tool-button mt5">
            <el-date-picker
                    v-model="searchDate"
                    type="datetimerange"
                    :picker-options="pickerOptions"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    align="right"
                    size="medium"
                    :default-time="['00:00:00', '23:59:59']"
                    value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
        </div>
        <!--搜索框-->
        <div class="btn-group tool-button mt5">
            <el-input v-model="pageForm.groupName" @keyup.enter.native="doSearch" placeholder="群名"
                      size="medium"></el-input>
        </div>
        <!--搜索框-->
        <div class="btn-group tool-button mt5">
            <el-input v-model="pageForm.userKeyword" @keyup.enter.native="doSearch" placeholder="昵称或者用户名"
                      size="medium"></el-input>
        </div>
        <!--按状态搜索-->
        <div class="btn-group tool-button mt5">
            <el-select v-model="pageForm.status" placeholder="请选择状态"
                       size="medium"
            >
                <el-option
                        v-for="item in statusOptions"
                        :key="item.value"
                        :label="item.name"
                        :value="item.value"
                >
                </el-option>
            </el-select>
        </div>
        <!--搜索按钮-->
        <div class="btn-group tool-button mt5">
            <el-button slot="append" icon="el-icon-search" @click="doSearch" size="medium"></el-button>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                size="small"
                header-align="center"
                style="width: 100%">
            <el-table-column
                    label="群名"
                    prop="groupName"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>
            <el-table-column
                    label="用户昵称"
                    prop="nickName"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="userName"
                    label="用户名"
                    header-align="center"
                    align="center">
            </el-table-column>
            <el-table-column
                    label="申请内容"
                    prop="content"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
                <template slot-scope="scope">
                    <el-button type="text" @click="contentDetail(scope.row.content)">
                        {{scope.row.content}}
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column
                    prop="ctAt"
                    label="申请时间"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    {{formatAt(scope.row.ctAt)}}
                </template>
            </el-table-column>
            <el-table-column
                    prop="status"
                    label="状态"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    {{formatStatus(scope.row.status)}}
                </template>
            </el-table-column>
            <!--操作下拉菜单-->
            <el-table-column
                    label="操作"
                    width="120"
                    align="center">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item :command="{type:'agree',data:scope.row}"
                                              v-if="scope.row.status == 'APPLYING'">
                                同意
                            </el-dropdown-item>
                            <el-dropdown-item :command="{type:'refuse',data:scope.row}"
                                              v-if="scope.row.status == 'APPLYING'">
                                拒绝
                            </el-dropdown-item>
                            <el-dropdown-item :command="{type:'delete',id:scope.row.id}"
                                              v-if="scope.row.status != 'APPLYING'">
                                删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <!--申请内容-->
    <el-dialog
            title="申请内容详情"
            :visible.sync="dialogVisible"
            width="30%">
        <span>{{contentItem}}</span>
    </el-dialog>

    <!--分页容器-->
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>

</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            var self = this;
            return {
                pickerOptions: {
                    disabledDate: function (time) {
                        return time.getTime() > new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000);
                    },
                    shortcuts: [{
                        text: '最近一周',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'weeks').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-3, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                dialogVisible: false,
                contentItem: "",
                searchDate: [],
                tableData: [],
                pageForm: {
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    userType: "",
                    searchKeyword: "",
                    searchDate: "",
                },
                formData: {
                    id:'',
                    groupId:'',
                    userId:'',
                    content:'',
                    status:'',
                },
                statusOptions: [],
            }
        },
        methods: {
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm:ss");
                return "";
            },
            // 枚举值转中文
            formatStatus: function (val) {
                for (const item of this.statusOptions) {
                    if (item.value === val) {
                        return item.name;
                    }
                }
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                if (self.searchDate) {
                    self.pageForm.searchDate = self.searchDate.toString();
                } else {
                    self.pageForm.searchDate = "";
                }
                $.post(base + "/platform/user/group/apply/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code === 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            contentDetail: function (content) {
                this.dialogVisible = true;
                this.contentItem = content;
            },
            dropdownCommand: function (command) {//监听下拉框事件
                var self = this;
                self.formData = command.data;
                // 同意按钮
                if ("agree" === command.type) {
                    self.$confirm('确认同意申请？ ', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" === a) {//确认后再执行
                                $.post(base + "/platform/user/group/apply/agree", self.formData, function (data) {
                                    if (data.code === 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.doSearch();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
                // 拒绝按钮
                else if ("refuse" === command.type) {
                    self.$confirm('确认拒绝申请？ ', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" === a) {//确认后再执行
                                $.post(base + "/platform/user/group/apply/refuse", self.formData, function (data) {
                                    if (data.code === 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.doSearch();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
                // 删除按钮
                else if ("delete" === command.type) {
                    self.$confirm('确认删除本条记录？ ', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" === a) {//确认后再执行
                                $.post(base + "/platform/user/group/apply/delete/" + command.id, {}, function (data) {
                                    if (data.code === 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.doSearch();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
            },
            // 状态枚举列表
            getStatus: function () {
                var _this = this;
                $.post(base + "/platform/user/group/apply/status", function (data) {
                    if (data.code === 0) {
                        _this.statusOptions = data.data;
                        _this.statusOptions.push({
                            id: '',
                            name: '全部状态'
                        });
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json")
            },
        },
        created: function () {
            this.pageData();
            this.getStatus();
        }
    });
</script>

<!--#
}
#-->